

package pe.gob.mef.logistica.security.user;

import org.acegisecurity.GrantedAuthority;
import org.acegisecurity.GrantedAuthorityImpl;
import org.acegisecurity.providers.encoding.ShaPasswordEncoder;
import org.acegisecurity.userdetails.User;
import org.acegisecurity.userdetails.UserDetails;
import org.acegisecurity.userdetails.UserDetailsService;
import org.acegisecurity.userdetails.UsernameNotFoundException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jlis.core.config.MessageConfig;
import org.jlis.core.util.HashUtil;
import org.jlis.core.util.UtilPasswordBase64encoding;
import org.jlis.ibatis.dao.DataUserIbatisDao;
import org.jlis.jdbc.logic.JdbcLogic;
import org.springframework.dao.DataAccessException;

import pe.gob.mef.logistica.web.util.ConstantesLogistica;


public class JdbcUserDetailsServiceImpl implements UserDetailsService {
	   protected final Log log = LogFactory.getLog(getClass());
    private ShaPasswordEncoder passwordEncoder;
    
    private DataUserIbatisDao dataUserIbatisDao;
    public JdbcUserDetailsServiceImpl() {
    }
    
   
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
        UtilPasswordBase64encoding util= new UtilPasswordBase64encoding();
        HashUtil filter = new HashUtil();
        filter.put("user", username.toUpperCase());
  
        log.info("codificando la clave en base64 " + util.encodeBase64("123"));
        
        String password =  util.decodeBase64(dataUserIbatisDao.getUser(filter));
        filter.put("pAplica", ConstantesLogistica.NOMBRE_APLICACION);
      
        String aplix=dataUserIbatisDao.getXApli(filter);
        filter.put("xApli", aplix);
        filter.put("pUsuario", username.toUpperCase());
        filter.put("pPass", util.encodeBase64(password.getBytes()));
        
        String existe=dataUserIbatisDao.getExiste(filter);
        filter.put("VALOR", existe);   
        
        if (filter==null || filter.size()==0) {
            throw new UsernameNotFoundException(MessageConfig.getMessageConfig().getProperty("security.acegi.applicationUnavailable"));
        } else if (filter.getString("VALOR").equalsIgnoreCase("0")) {
            throw new UsernameNotFoundException(MessageConfig.getMessageConfig().getProperty("security.acegi.invalidUserPassword"));
        }
        
        // Obtener los roles y colocarlos en el arreglo "grantedAuthoryties"
        GrantedAuthority [] grantedAuthorities = { new GrantedAuthorityImpl("ROLE_USER") };
        String encodedPassword = this.passwordEncoder.encodePassword(password, null);
        UserDetails userDetails = new User(username, encodedPassword, true, true, true, true, grantedAuthorities);
        return userDetails;
    }
    
    public void setPasswordEncoder(ShaPasswordEncoder passwordEncoder) {
        this.passwordEncoder = passwordEncoder;
    }
    
    public ShaPasswordEncoder getPasswordEncoder() {
        return this.passwordEncoder;
    }
    
    public void setDataUserIbatisDao(DataUserIbatisDao dataUserIbatisDao) {
        this.dataUserIbatisDao = dataUserIbatisDao;
    }
    
}
